
layui.use(['element', 'layer'], function () {

});

$(function () {
    let firstReadyTime = parseInt(getQueryVariable('firstReadyTime'))
    let firstAnswerTime = parseInt(getQueryVariable('firstAnswerTime'))
    let firstReadyText = formatSeconds(firstReadyTime)
    let firstAnswerText = formatSeconds(firstAnswerTime)
    $('#firstReadyText').text(firstReadyText)
    $('#firstAnswerText').text(firstAnswerText)

    let secondReadyTime = parseInt(getQueryVariable('secondReadyTime'))
    let secondAnswerTime = parseInt(getQueryVariable('secondAnswerTime'))
    let secondReadyText = formatSeconds(secondReadyTime)
    let secondAnswerText = formatSeconds(secondAnswerTime)
    $('#secondReadyText').text(secondReadyText)
    $('#secondAnswerText').text(secondAnswerText)

    let thirdReadyTime = parseInt(getQueryVariable('thirdReadyTime'))
    let thirdAnswerTime = parseInt(getQueryVariable('thirdAnswerTime'))
    let thirdReadyText = formatSeconds(thirdReadyTime)
    let thirdAnswerText = formatSeconds(thirdAnswerTime)
    $('#thirdReadyText').text(thirdReadyText)
    $('#thirdAnswerText').text(thirdAnswerText)

    let totalTime = parseInt(getQueryVariable('totalTime'))
    let totalAnswerTime = parseInt(getQueryVariable('totalAnswerTime'))

    let totalText = formatSeconds(totalTime)
    $('#totalText').text(totalText)

    let totalAnswerText = formatSeconds(totalAnswerTime)
    $('#totalAnswerText').text(totalAnswerText)


    let AnswerTimePercent = parseInt((totalAnswerTime / (totalTime)) * 100)
    AnswerTimePercent = MistakeToZero(AnswerTimePercent)
    $('#AnswerTimePercent').text(AnswerTimePercent)

    // 分别获得三次的卡顿次数
    let firstPause = parseInt(getQueryVariable('firstPause'))
    let secondPause = parseInt(getQueryVariable('secondPause'))
    let thirdPause = parseInt(getQueryVariable('thirdPause'))
    let totalPause = firstPause + secondPause + thirdPause
    $('#totalPause').text(totalPause)
    $('#firstPauseCount').text(firstPause)
    $('#secondPauseCount').text(secondPause)
    $('#thirdPauseCount').text(thirdPause)


    let firstPausePercent = (firstPause / (firstAnswerTime / 30)).toFixed(2)
    let secondPausePercent = (secondPause / (secondAnswerTime / 30)).toFixed(2)
    let thirdPausePercent = (thirdPause / (thirdAnswerTime / 30)).toFixed(2)

    firstPausePercent = MistakeToZero(firstPausePercent)
    secondPausePercent = MistakeToZero(secondPausePercent)
    thirdPausePercent = MistakeToZero(thirdPausePercent)


    $('#firstPausePercent').text(firstPausePercent)
    $('#secondPausePercent').text(secondPausePercent)
    $('#thirdPausePercent').text(thirdPausePercent)

    let fileName = decodeURI(getQueryVariable('fileName'))
    let fileNameNoPostfix = fileNameTrim(fileName);
    $('#title').text(fileNameNoPostfix)

})


//对文件名进行裁剪
//如 '0927下午_市政建设_全.mp3' -> '0927下午_市政建设'
function fileNameTrim(fileName) {
    let fileNameNoPostfix = fileName.substring(0, fileName.lastIndexOf("."))
    let reg = RegExp(/._全$/g);
    if (fileNameNoPostfix.match(reg)) {
        fileNameNoPostfix = fileNameNoPostfix.substring(0,fileNameNoPostfix.length-2)
    }
    return fileNameNoPostfix
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

//将秒数转换为时分秒格式
function formatSeconds(value) {
    var theTime = parseInt(value);// 秒

    if(theTime === 0){
        return '0秒钟'
    }

    var middle = 0;// 分
    var hour = 0;// 小时

    if (theTime > 60) {
        middle = parseInt(theTime / 60);
        theTime = parseInt(theTime % 60);
        if (middle > 60) {
            hour = parseInt(middle / 60);
            middle = parseInt(middle % 60);
        }
    }
    let theTimeNum = parseInt(theTime)
    // 出现 1分5秒 前补零为 1分05秒
    let result = "" + (Array(2).join(0) + theTimeNum).slice(-2) + "秒";
    if (theTimeNum === 0) {
        result = "钟"
    }
    if (middle > 0) {
        result = "" + parseInt(middle) + "分" + result;
    }
    if (hour > 0) {
        result = "" + parseInt(hour) + "小时" + result;
    }
    return result;
}

function onCopyBtn() {
    let title = $("#title").text();
    let totalText = $("#totalText").text();
    let totalAnswerText = $("#totalAnswerText").text();

    let firstReadyText = $("#firstReadyText").text();
    let firstAnswerText = $("#firstAnswerText").text();
    let firstPauseCount = $("#firstPauseCount").text();

    let secondReadyText = $("#secondReadyText").text();
    let secondAnswerText = $("#secondAnswerText").text();
    let secondPauseCount = $("#secondPauseCount").text();


    let thirdReadyText = $("#thirdReadyText").text();
    let thirdAnswerText = $("#thirdAnswerText").text();
    let thirdPauseCount = $("#thirdPauseCount").text();

    let AnswerTimePercent = $("#AnswerTimePercent").text();
    let totalPause = $("#totalPause").text();
    let firstPausePercent = $("#firstPausePercent").text();
    let secondPausePercent = $("#secondPausePercent").text();
    let thirdPausePercent = $("#thirdPausePercent").text();

    let content = title + '\n'

    content += '共' + totalText + '，答题' + totalAnswerText + '\n'
    content += '第一题：准备' + firstReadyText + '，答题' + firstAnswerText
        + '，卡顿 '+firstPauseCount + '次\n'
    content += '第二题：准备' + secondReadyText + '，答题' + secondAnswerText
        + '，卡顿 '+secondPauseCount + '次\n'
    content += '第三题：准备' + thirdReadyText + '，答题' + thirdAnswerText
        + '，卡顿 '+thirdPauseCount + '次\n\n'
    content += '分析：\n'
    content += '答题时长占总时长的' + AnswerTimePercent + '%，共卡顿' + totalPause + '次\n'
    content += '第一题每30秒卡顿' + firstPausePercent + '次\n'
    content += '第二题每30秒卡顿' + secondPausePercent + '次\n'
    content += '第三题每30秒卡顿' + thirdPausePercent + '次\n'
    copy(content); //传递文本
    //提示层
    layer.msg('复制成功');
}


function copy(value) {
    let transfer = document.createElement('textarea');
    document.body.appendChild(transfer);
    transfer.value = value;  // 这里表示想要复制的内容
    transfer.focus();
    transfer.select();
    if (document.execCommand('copy')) {
        document.execCommand('copy');
    }
    transfer.blur();
    document.body.removeChild(transfer);

}

//错误值：NaN、null和undefined转0
function MistakeToZero(obj) {
    if(isNaN(obj)){
        obj = 0
    }


    if (!obj){
        obj = 0
    }
    return obj
}